﻿using AutoMapper;
using MediatR;
using MySqlX.XDevAPI.Common;
using Warehouse.Api.Write.AllGCotion.Command;
using Warehouse.Domian.Model;
using Warehouse.ErrorCode;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Api.Write.AllGCotion.Hanlder
{
    public class AddBasicCommandHanlder : IRequestHandler<AddBasicCommand, ApiResult<int>>
    {
        DbContext db;
        ILogger<AddBasicCommandHanlder> logger;
        IMapper mapper;

        public AddBasicCommandHanlder(DbContext db, ILogger<AddBasicCommandHanlder> logger, IMapper mapper)
        {
            this.db = db;
            this.logger = logger;
            this.mapper = mapper;
        }
        /// <summary>
        /// 添加基本信息的方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async  Task<ApiResult<int>> Handle(AddBasicCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                var carrier = mapper.Map<Basicinformatio>(request);

                int r = await db.SqlSugar.Insertable(carrier).ExecuteCommandAsync();
                if (r > 0)
                {
                    result.Msg = "库存基本信息添加成功";
                    result.Data = r;
                    result.Code = ApiEume.Success;
                    logger.LogInformation(result.Msg);
                }
                else
                {
                    result.Msg = "库存基本信息添加失败";
                    result.Data = r;
                    result.Code = ApiEume.Error;
                    logger.LogError(result.Msg);
                }
                return await Task.FromResult(result);
            }
            catch (Exception ex)
            {

            }
            return result;
        }
    }
}
